public class BinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root = null;

    public boolean search(int val) {
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val < val) {
                cur = cur.right;
            } else if (cur.val > val) {
                cur = cur.left;
            } else {
                return true;
            }
        }
        return false;
    }

    public boolean insert(int val) {
        if(root == null) {
            root = new TreeNode(val);
            return true;
        }
        TreeNode parent = null;
        TreeNode cur = root;
        while(cur != null) {
            if(cur.val > val) {
                parent = cur;
                cur = cur.left;
            }else if(cur.val < val) {
                parent = cur;
                cur = cur.right;
            }else {
                return false;
            }
        }
        if(parent.val > val) {
            parent.left = new TreeNode(val);
        }else {
            parent.right = new TreeNode(val);
        }
        return true;
    }

}